package com.dx_society.controller;

import com.dx_society.dto.Result;
import com.dx_society.service.SocietiesMemberService;
import com.dx_society.service.SocietiesService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/so")
public class SocietiesController {
    @Resource
    private SocietiesService societiesService;
    @Resource
    private SocietiesMemberService societiesMemberService;
    /**
     * 获取社团列表
     * @param type 社团类型（非必填）
     * @param name 社团名称（非必填）
     * @param orderRole 排序方式（默认0）0：默认排序，1：按人数降序，2：按人数升序，3：按社团名称升序，4：按社团名称降序
     * @return 符合条件的社团列表
     */
    @GetMapping(value = "/list")
    public Result querySocietiesList(
            @RequestParam(value = "type", required = false) String type,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "order", defaultValue = "0") String orderRole
            ) {
        Integer orderRoleInt = Integer.parseInt(orderRole);
        return societiesService.querySocietiesList(type, name, orderRoleInt);
    }

    /**
     * 获取社团详情
     * @param id 社团id
     * @return 社团详情
     */
    @GetMapping(value = "/detail/{id}")
    public Result querySocietiesDetail(@PathVariable("id") Long id) {
        return societiesService.querySocietiesDetail(id);
    }

    /**
     * 请求加入
     * @param id 社团id
     * @param joinReason 加入理由
     * @return 操作结果
     */
    @PostMapping(value = "/join")
    public Result joinSocieties(@RequestParam("id") Long id,
                                @RequestParam(value = "joinReason")String joinReason) {
        return societiesMemberService.joinSocieties(id, joinReason);
    }
    /**
     * 请求退部
     * @param id 社团id
     * @param leaveReason 退部理由
     * @return 操作结果
     */
    @PostMapping(value = "/leave")
    public Result leaveSocieties(@RequestParam("id") Long id,
                                 @RequestParam(value = "leaveReason")String leaveReason) {
        return societiesMemberService.leaveSocieties(id, leaveReason);
    }

    /**
     * 获取社团公告
     * @param id 社团id
     * @return 社团公告
     */
    @GetMapping(value = "/notice")
    public Result querySocietyNotice(@RequestParam("id")Long id) {
        return societiesService.querySocietyNotice(id);
    }

    /**
     * 获取我加入的社团列表
     * @param state 状态（0：申请入部，1：已入部，2：申请退部）
     * @return 我的社团列表
     */
    @GetMapping(value = "/my/list")
    public Result queryMySocietyList(@RequestParam(value = "state") Integer state) {
        return societiesMemberService.queryMySocietyList(state);
    }

    /**
     * 获取某个社团成员列表
     * @return 我的社团成员列表
     */
    @GetMapping(value = "/society/members")
    public Result querySocietyMembers(@RequestParam("id") Long id) {
        return Result.ok(societiesMemberService.query().eq("societies_id", id).list());
    }


    @GetMapping(value = "/list/brief")
    public Result querySocietyBrief() {
        return societiesService.querySocietyBrief();
    }
}
